<?php

namespace app\sys\controller;

use think\Db;

// 后台 系统设置
class Setting extends Admin
{
    //业务员（客服）列表
    public function index()
    {
        $name = input('param.name', '');
        $phone = input('param.phone', '');
        $page = input('param.page', 1);
        $page = $page <= 0 ? 1 : $page;
        $limit = input('param.limit', 20);
        $order_field = input('param.field', '');
        $order_type = input('param.order', 0);
        if (request()->isAjax()) {
            $query = ['permission' => 3];
            if ($name) {
                $query['nickname|username'] = ['like', $name];
            }
            if ($phone) {
                $query['phone'] = ['like', $phone];
            }
            $order = [];
            if ($order_field && $order_type) {
                $order[$order_field] = 'asc' == $order_type ? 1 : -1;
            }
            $list = Db::name('admin')->where($query)->page($page)->limit($limit)->order($order)->select();
            $count = Db::name('admin')->where($query)->count();

            return success(['data' => $list, 'count' => $count]);
        }

        return view('customerService', ['page' => $page, 'limit' => $limit, 'name' => $name, 'phone' => $phone]);
    }

    //业务员（客服）添加\编辑
    public function customerServiceEdit()
    {
        if (request()->isPost()) {
            $data = input('param.', '');
            $row = [];
            $row['username'] = strval($data['username']);
            $row['phone'] = strval($data['phone']);
            $row['qq'] = strval($data['qq']);
            $row['nickname'] = strval($data['nickname']);
            $row['remark'] = strval($data['remark']);
            $row['push_money'] = round($data['push_money'], 2);
            if (24 == strlen($data['id'])) {
                Db::name('admin')->where('id', $data['id'])->update($row);
                $admin_id = $data['id'];
            } else {
                $find = Db::name('admin')->where('phone', $row['phone'])->find();
                if ($find) {
                    return error('该手机号已经存在');
                }
                $row['permission'] = 3;
                $row['avatar'] = config('file_domain').'static/images/default_avatar.png';
                $admin_id = Db::name('admin')->insert($row);                
            }

            //获取专属二维码
            $qr = ROOT_PATH.'public/qrcode/csid_'.$admin_id.'.png';
            $wxs = new \wx\WeixinService();
            $path = 'pages/gangManage/create/create';
            $result = $wxs->getQrCodeUnlimit($path, 325, $admin_id);
            file_put_contents($qr, $result);
            Db::name('admin')->where('id', $admin_id)->update(['qrcode' => config('file_domain').'qrcode/csid_'.$admin_id.'.png']);

            return success();
        }

        $id = input('param.id', '');
        $data = Db::name('admin')->find($id);

        return view('customerServiceEdit', $data);
    }

    //业务员（客服）删除
    public function customerServiceDel()
    {
        if (request()->isPost()) {
            $admin_info = session('admin_info');
            if ($admin_info['permission'] < 10) {
                return error('你的权限不够');
            }
            $id = input('param.id', '');
            Db::name('admin')->delete($id);

            return success();
        }

        return error();
    }

    //业务员 重置密码
    public function customerServiceResetPwd()
    {
        if (request()->isPost()) {
            $admin_info = session('admin_info');
            if ($admin_info['permission'] < 10) {
                return error('你的权限不够');
            }
            $id = input('param.id', '');
            Db::name('admin')->where('id', $id)->update(['password'=> md5('123456'.config('api_secret'))]);

            return success();
        }

        return error();
    }

    // 修改密码
    public function passwordEdit()
    {
        if (request()->isPost()) {
            $id = input('param.id', '');
            $admin_info = session('admin_info');
            if ($admin_info['permission'] < 10 && $admin_info['id'] != $id) {
                return error('你的权限不够');
            }
            $old_password = input('param.old_password', '');
            $new_password = input('param.new_password', '');
            $con_password = input('param.con_password', '');
            if ($con_password != $new_password) {
                return error('两次新密码输入不正确');
            }
            $old_password = md5($old_password.config('api_secret'));
            $find = Db::name('admin')->find($id);
            if ($find['password'] != $old_password) {
                return error('旧密码错误');
            }

            Db::name('admin')->where('id', $id)->update(['password' => md5($new_password.config('api_secret'))]);

            return success();
        }

        return view('passwordEdit');
    }

    // 帮会类别列表
    public function gangType()
    {
        $name = input('param.name', '');
        $page = input('param.page', 1);
        $page = $page <= 0 ? 1 : $page;
        $limit = input('param.limit', 20);
        $order_field = input('param.field', '');
        $order_type = input('param.order', 0);
        if (request()->isAjax()) {
            $query = [];
            if ($name) {
                $query['name'] = ['like', $name];
            }
            $order = [];
            if ($order_field && $order_type) {
                $order[$order_field] = 'asc' == $order_type ? 1 : -1;
            }
            $order['ct'] = 1;
            $list = Db::name('gang_type')->where($query)->page($page)->limit($limit)->order($order)->select();
            $count = Db::name('gang_type')->where($query)->count();

            return success(['data' => $list, 'count' => $count]);
        }

        return view('gangType', ['page' => $page, 'limit' => $limit, 'name' => $name]);
    }

    // 帮会类别添加编辑
    public function gangTypeEdit()
    {
        if (request()->isPost()) {
            $data = input('param.', '');
            $data['raise_price'] = round($data['raise_price'], 2);
            $id = $data['id'];
            if (!$id) {
                $data['ct'] = time();
            }
            unset($data['id']);
            foreach ($data['raise_price_list'] as $key => $value) {
                foreach ($value as $k => $v) {
                    $value[$k] = round($v, 2);
                }
                $data['raise_price_list'][$key] = $value;
            }
            //存在修改 不存在添加
            Db::name('gang_type')->where('id', $id)->update($data, ['upsert' => true]);

            return success();
        }

        $id = input('param.id', '');
        $data = Db::name('gang_type')->find($id);
        $brand = Db::name('brand')->select();
        $type = Db::name('type')->select();

        return view('gangTypeEdit', ['info' => $data, 'brand' => $brand, 'type' => $type]);
    }

    // 帮会类别 设置默认
    public function gangTypeDefault()
    {
        if (request()->isPost()) {
            $data = input('param.', '');
            $id = $data['id'];
            $checked = $data['checked'];
            Db::name('gang_type')->where('id', 'neq', $id)->update(['default' => 0]);
            Db::name('gang_type')->where('id', $id)->update(['default' => 'true' == $checked ? 1 : 0]);

            return success();
        }

        return error();
    }

    // 帮会类别删除
    public function gangTypeDel()
    {
        if (request()->isPost()) {
            $admin_info = session('admin_info');
            if ($admin_info['permission'] < 10) {
                return error('你的权限不够');
            }
            $id = input('param.id', '');
            Db::name('gang_type')->delete($id);

            return success();
        }

        return error();
    }

    // 公告列表
    public function noticeList()
    {
        $name = input('param.name', '');
        $page = input('param.page', 1);
        $page = $page <= 0 ? 1 : $page;
        $limit = input('param.limit', 20);
        $order_field = input('param.field', '');
        $order_type = input('param.order', 0);
        if (request()->isAjax()) {
            $query = [];
            if ($name) {
                $query['title'] = ['like', $name];
            }
            $order = ['time' => -1];
            if ($order_field && $order_type) {
                $order[$order_field] = 'asc' == $order_type ? 1 : -1;
            }
            $list = Db::name('notice')->where($query)->page($page)->limit($limit)->order($order)->select();
            foreach ($list as $key => $value) {
                if ($value['user_id']) {
                    $user_info = Db::name('user')->find($value['user_id']);
                    $list[$key]['user_name'] = $user_info['name'];
                }
                if ($value['gang_id']) {
                    $gang_info = Db::name('gang')->find($value['gang_id']);
                    $list[$key]['gang_name'] = $gang_info['name'];
                }
                $list[$key]['time'] = date('Y-m-d H:i:s', $value['time']);
            }
            $count = Db::name('notice')->where($query)->count();

            return success(['data' => $list, 'count' => $count]);
        }

        return view('noticeList', ['page' => $page, 'limit' => $limit, 'name' => $name]);
    }

    // 公告编辑
    public function noticeEdit()
    {
        if (request()->isPost()) {
            $data = input('param.', '');
            $data['time'] = strtotime($data['time']);
            $data['over_time'] = strtotime($data['over_time']);
            $data['ct'] = time();
            $data['is_top'] = intval($data['is_top']);
            $data['come_from'] = '平台';

            $id = $data['id'];
            unset($data['id']);
            //存在修改 不存在添加
            Db::name('notice')->where('id', $id)->update($data, ['upsert' => true]);

            return success();
        }

        $id = input('param.id', '');
        if ($id) {
            $data = Db::name('notice')->find($id);
            if ($data['user_id']) {
                $user_info = Db::name('user')->find($data['user_id']);
                $data['user_name'] = $user_info['name'];
            }
            if ($data['gang_id']) {
                $gang_info = Db::name('gang')->find($data['gang_id']);
                $data['gang_name'] = $gang_info['name'];
            }
            $data['time'] = date('Y-m-d H:i:s', $data['time']);
        } else {
            $data['time'] = date('Y-m-d H:i:s');
            $data['over_time'] = date('Y-m-d H:i:s', strtotime($data['time'].' +1 month -1 day'));
        }

        return view('noticeEdit', $data);
    }

    // 公告删除
    public function noticeDel()
    {
        if (request()->isPost()) {
            $admin_info = session('admin_info');
            if ($admin_info['permission'] < 10) {
                return error('你的权限不够');
            }
            $id = input('param.id', '');
            Db::name('notice')->delete($id);

            return success();
        }

        return error();
    }

    // 供应商列表
    public function supplier()
    {
        $name = input('param.name', '');
        $phone = input('param.phone', '');
        $csid = input('param.csid', '');
        $date = input('param.date', '');
        $page = input('param.page', 1);
        $page = $page <= 0 ? 1 : $page;
        $limit = input('param.limit', 20);
        $order_field = input('param.field', '');
        $order_type = input('param.order', 0);
        if (request()->isAjax()) {
            $query['permission'] = 6;
            if ($name) {
                $query['name'] = ['like', $name];
            }
            if ($phone) {
                $query['phone'] = ['like', $phone];
            }
            $order = ['time' => -1];
            if ($order_field && $order_type) {
                $order[$order_field] = 'asc' == $order_type ? 1 : -1;
            }
            $list = Db::name('admin')->where($query)->page($page)->limit($limit)->order($order)->select();
            $count = Db::name('admin')->where($query)->count();

            return success(['data' => $list, 'count' => $count]);
        }

        return view('supplier');
    }

    // 供应商编辑
    public function supplierEdit()
    {
        if (request()->isPost()) {
            $data = input('param.', '');
            $row = [];
            $row['phone'] = strval($data['phone']);
            if (!checkPhone($row['phone'])) {
                return error('请正确填写供应商手机号');
            }
            $id = $data['id'];
            if (!$id) {
                $find = Db::name('admin')->where('phone', $row['phone'])->find();
                if ($find) {
                    return error('该手机号已经存在');
                }
            }
            $row['username'] = strval($data['username']);
            $row['password'] = strval($data['password']);
            $row['password'] = $row['password'] ? $row['password'] : '111111';
            $row['password'] = md5($row['password'].config('api_secret'));
            $row['permission'] = 6;
            //存在修改 不存在添加
            Db::name('admin')->where('id', $id)->update($row, ['upsert' => true]);

            return success();
        }

        $id = input('param.id', '');
        $data = Db::name('admin')->find($id);

        return view('supplierEdit', $data);
    }
}
